package com.dorje.sys.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.druid.util.StringUtils;
import com.dorje.sys.entity.User;
import com.dorje.sys.service.UserService;

@Controller
@RequestMapping("/user/")
public class UserController extends BaseController<User> {

	private final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserService userService;
	
	/*@RequestMapping("list")
	public void list(@ModelAttribute User user,HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer pageNum = 1;
		Integer rows = 10;
		try {
			pageNum = Integer.valueOf(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		Page<User> page = PageHelper.startPage(pageNum, rows);
		List<User> users = userService.findAll(user);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", users);
		map.put("total", page.getTotal());
		String str = JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd HH:mm:ss.SSS");
		response.getWriter().write(str);
	}*/

	@RequestMapping("add")
	public String add(Model model) {
		model.addAttribute("user", new User());
		return "sys/user/info";
	}

	@RequestMapping(value = "edit", method = RequestMethod.GET)
	public String edit(@RequestParam("id") Integer id, Model model) {
		model.addAttribute("user", userService.findInfoById(id));
		return "sys/user/info";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public void save(@ModelAttribute User user, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String msg = userValidation(user, request);
		if (msg == null) {
			if(user.getId() > 0 ){
				User u = userService.findInfoById(user.getId());
				if(u.getId() > 0){
					if(StringUtils.isEmpty(user.getPassword())){
						user.setPassword(u.getPassword());
					}
					userService.update(user);
					msg = "更新用户成功";
				}else{
					msg = "信息不存在";
				}
			}else{
				userService.insert(user);
				msg = "添加用户成功";
			}
		} 
		response.getWriter().write(msg);
	}
	
	public String userValidation(User user, HttpServletRequest request) {
		String msg = null;
		if(user.getId() < 1 || user.getPassword().length() > 0 ){
			if (user.getPassword().length() < 3) {
				msg = "密码至少3位";
			} else if (!StringUtils.equals(user.getPassword(), request.getParameter("repassword"))) {
				msg = "两次密码不相等";
			}else{
				user.setPassword(DigestUtils.sha1Hex(user.getPassword()));
			}
		} else if (StringUtils.isEmpty(user.getEmail())) {
			msg = "请输入邮箱";
		} else if (StringUtils.isEmpty(user.getName())) {
			msg = "姓名不能为空";
		}else if (StringUtils.isEmpty(user.getAccount())){
			msg = "帐号不能为空";
		}else if(userService.checkAccount(user) > 0){
			msg="帐号已存在";
		}
		return msg;
	}

	@RequestMapping("index")
	public String index() {
		System.out.println(userService.findPermByUserId(1));
		;
		return "sys/user";
	}
}
